﻿/**
 * ...
 * @author alijaya
 */

package homponex.core;

#if flash9
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
#end

import heox.Rectangle;
import homponex.utils.StringUtils;
import homponex.utils.DynamicUtils;

class Panel extends Component
{

	private var childs:List<Component>;
	
	#if flash
	private var background:S9G;
	
	private var bCon:Sprite;
	private var cCon:Sprite;
	private var mask:Shape;
	#end
	
	////////////
	// Scroll //
	////////////
	
	private var _scrollH : Float;
	public var scrollH(get_scrollH, set_scrollH) : Float;
	
	private function get_scrollH() : Float
	{
		return _scrollH;
	}
	
	private function set_scrollH(value:Float) : Float
	{
		_scrollH = value;
		update_scrollH();
		return value;
	}
	
	private var _scrollV : Float;
	public var scrollV(get_scrollV, set_scrollV) : Float;
	
	private function get_scrollV() : Float
	{
		return _scrollV;
	}
	
	private function set_scrollV(value:Float) : Float
	{
		_scrollV = value;
		update_scrollV();
		return value;
	}
	
	
	/////////////////
	// Child Place //
	/////////////////
	
	private var dTop:Float;
	private var dBottom:Float;
	private var dLeft:Float;
	private var dRight:Float;
	
	public var initRect(default, null):Rectangle;
	public var rect(default, null):Rectangle;
	
	/////////////////
	// Constructor //
	/////////////////
	public function new()
	{
		super();
		childs = new List<Component>();
		
		_scrollH = 0;
		_scrollV = 0;
		
		#if flash9
		bCon = new Sprite();
		cCon = new Sprite();
		mask = new Shape();
		mask.graphics.beginFill(0x000000);
		mask.graphics.drawRect(0, 0, 1, 1);
		cCon.mask = mask;
		
		view.addChild(bCon);
		view.addChild(cCon);
		view.addChild(mask);
		#end
		
		setStyle(Style.defaultStyle.getStyle(DynamicUtils.getClassTree(Type.getClass(this))));
		setDim(initWidth, initHeight);
	}
	
	public override function setStyle(s:Dynamic)
	{
		super.setStyle(s);
		
		var rP:Rectangle;
		var rC:Rectangle;
		
		#if flash9
		if (background != null) bCon.removeChild(background.view);
		background = cast(DynamicUtils.notNull(style.background, Style.skinWhite).clone());
		bCon.addChild(background.view);
		rP = background;
		rC = background.initRect;
		#end
		
		initWidth = rP.initWidth;
		initHeight = rP.initHeight;
		
		initRect = cast(rC.clone());
		
		dTop = initRect.top;
		dBottom = initHeight - initRect.bottom;
		dLeft = initRect.left;
		dRight = initWidth - initRect.right;
		
		rect = cast(initRect.clone());
		
		setPosDim(x, y, width, height);
	}
	
	///////////////////////
	// Hierarcy Function //
	///////////////////////
	public function addChild(c:Component)
	{
		if (c.parent == this)
		{
			if (!existsChild(c))
			{
				childs.add(c);
				update_add(c);
			}
		} else
		{
			childs.add(c);
			c.addTo(this);
			update_add(c);
		}
	}
	
	public function removeChild(c:Component)
	{
		if (c.parent == this)
		{
			childs.remove(c);
			c.removeFrom();
			update_remove(c);
		} else if(c.parent == null)
		{
			childs.remove(c);
			update_remove(c);
		}
	}
	
	public function existsChild(child:Component) : Bool
	{
		for (c in childs)
		{
			if (c == child) return true;
		}
		return false;
	}
	
	/////////////////////
	// Update Function //
	/////////////////////
	
	private function update_add(c:Component)
	{
		#if flash9
		cCon.addChild(c.view);
		#end
	}
	
	private function update_remove(c:Component)
	{
		#if flash9
		cCon.removeChild(c.view);
		#end
	}
	
	
	private function update_scrollH()
	{
		#if flash9
		cCon.x = rect.x - (scrollH * (rect.width - cCon.width));
		#end
	}
	
	private function update_scrollV()
	{
		#if flash9
		cCon.y = rect.y - (scrollV * (rect.height - cCon.height));
		#end
	}
	
	
	private override function update_width()
	{
		rect.left = dLeft + DynamicUtils.notNull(style.padLeft, 0);
		rect.right = width - dRight - DynamicUtils.notNull(style.padRight, 0);
		
		#if flash9
		background.width = width;
		
		mask.x = rect.x;
		mask.width = rect.width;
		#end
		update_scrollH();
	}
	
	private override function update_height()
	{
		rect.top = dTop + DynamicUtils.notNull(style.padTop, 0);
		rect.bottom = height - dBottom - DynamicUtils.notNull(style.padBottom, 0);
		
		#if flash9
		background.height = height;
		
		mask.y = rect.y;
		mask.height = rect.height;
		#end
		update_scrollV();
	}
	
	///////////////////////
	// Get Tree Function //
	///////////////////////
	public override function getTree(?depth:Int = 0) : String
	{
		var s:String = super.getTree(depth);
		//s += StringUtils.nt(1, depth) + "  {";
		for (c in childs)
		{
			s += c.getTree(depth+1);
		}
		//s += StringUtils.nt(1, depth) + "  }";
		return s;
	}
	
}